package com.holyn.guofang.view.distinguish;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.holyn.guofang.MyApplication;
import com.holyn.guofang.R;
import com.holyn.guofang.viewcommon.multileveltreelist.Node;
import com.holyn.guofang.viewcommon.multileveltreelist.TreeHelper;
import com.holyn.guofang.viewcommon.multileveltreelist.TreeRecyclerAdapter;

import java.util.List;

/**
 * Created by Holyn on 2017/7/13.
 */

public class ProductGroupRecyclerAdapter extends TreeRecyclerAdapter {

    public ProductGroupRecyclerAdapter(RecyclerView mTree, Context context, List<Node> datas) {
        super(mTree, context, datas, 0, true);
    }

    @Override
    public void onBindViewHolder(Node node, RecyclerView.ViewHolder holder, int position) {
        if (node.getLevel() == 1){
            holder.itemView.setBackgroundColor(ContextCompat.getColor(MyApplication.getContext(), R.color.light_green));
        } else {
            holder.itemView.setBackgroundColor(Color.WHITE);
        }
        final MyHoder viewHolder = (MyHoder) holder;
        //todo do something
        viewHolder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setChecked(node,viewHolder.cb.isChecked());
            }
        });

        if (node.isChecked()){
            viewHolder.cb.setChecked(true);
        }else {
            viewHolder.cb.setChecked(false);
        }

        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }

        viewHolder.label.setText(node.getName());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHoder(LayoutInflater.from(mContext).inflate(R.layout.item_multi_tree_list, parent, false));
    }

    public String getSelectGroupId(){
        String ids = "";
        for (int i = 0; i < getAllReadyShowNodes().size(); i++){
            Node node = getAllReadyShowNodes().get(i);
            if (node.getLevel() == 1){
                if (node.isChecked() == true){
                    if (TextUtils.isEmpty(ids)){
                        ids = ids + node.getId();
                    } else {
                        ids = ids + ";" + node.getId();
                    }
                }
            }
        }
        return ids;
    }

    /**
     *  根据typeId，groupId来过滤列表
     *  注：Node对象里面的ID用的是typeId或者groupId
     */
    public void notifyAllNodesChange(String typeId, String groupId){
        getAllReadyShowNodes().clear();
        if (typeId.equals("所有")){
            getAllReadyShowNodes().addAll(getAllNodes());
        } else {
            if (groupId.equals("所有")){
                for (int i = 0; i < getAllNodes().size(); i++){
                    Node node = getAllNodes().get(i);
                    if (node.getId().equals(typeId) || node.getpId().equals(typeId)){
                        getAllReadyShowNodes().add(node);
                    }
                }
            } else {
                for (int i = 0; i < getAllNodes().size(); i++){
                    Node node = getAllNodes().get(i);
                    if (node.getId().equals(typeId) || node.getId().equals(groupId)){
                        getAllReadyShowNodes().add(node);
                    }
                }
            }
        }


        getShowNodes().clear();
        getShowNodes().addAll(TreeHelper.filterVisibleNode(getAllReadyShowNodes()));
        notifyDataSetChanged();
    }

    class MyHoder extends RecyclerView.ViewHolder{

        public CheckBox cb;

        public TextView label;

        public ImageView icon;
        public MyHoder(View itemView) {
            super(itemView);

            cb = (CheckBox) itemView
                    .findViewById(R.id.cb_select_tree);
            label = (TextView) itemView
                    .findViewById(R.id.tv_content);
            icon = (ImageView) itemView.findViewById(R.id.iv_expand);

        }

    }
}
